/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package test.se.fk.klistret.console;

import java.io.File;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.PosixParser;
import org.junit.Before;
import org.junit.Test;

import se.fk.klistret.console.exceptions.ApplicationException;
import se.fk.klistret.console.exceptions.InfrastructureException;

/**
 * 
 * @author Matthew Young
 *
 */
public class TextBasedClient {

	@Before
	public void setUp() throws Exception {
	}
	
	@SuppressWarnings("static-access")
	@Test
	public void run() {
		String[] args = new String[] {"-pC:\\workshop\\fk-klistret-extensions\\Klistret Java Console\\configuration\\example\\Test.properties"};
		
		Options options = new Options();

		Option usage = OptionBuilder.withLongOpt("usage").withDescription(
				"TextBasedClient usage").create();

		Option properties = OptionBuilder.withArgName("property file")
				.hasArgs().withDescription("Load all properties from file")
				.withLongOpt("propertyfile").create("p");

		options.addOption(usage);
		options.addOption(properties);

		CommandLineParser parser = new PosixParser();
		HelpFormatter formatter = new HelpFormatter();

		try {
			File propertyFile = null;

			CommandLine line = parser.parse(options, args);

			if (line.hasOption("usage")) {
				formatter.printHelp("TextBasedClient", options);
				System.exit(0);
			}

			if (line.hasOption("p")) {
				propertyFile = new File(line.getOptionValue("p"));
			} else {
				throw new InfrastructureException(
						"Option -p (property) is required to load local settings");
			}

			se.fk.klistret.console.TextBasedClient client = new se.fk.klistret.console.TextBasedClient(propertyFile);
			client.execute();
		} catch (org.apache.commons.cli.ParseException pa) {
			System.out
					.println("ParseException caught reading command line arguements: "
							+ pa.getMessage());
		} catch (InfrastructureException infe) {
			System.out.println("Infrastructure exception caught: "
					+ infe.getMessage());
		} catch (ApplicationException iste) {
			System.out.println("Application exception caught: "
					+ iste.getMessage());
		}
	}
}
